package com.zhkj.txg.module.mine.adapter

import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.entity.ExtendEntity
import java.math.BigDecimal

class DiamondMemberAdapter(val onItemClick: (extendEntity: ExtendEntity) -> Unit) :
    BaseQuickAdapter<ExtendEntity, BaseViewHolder>(R.layout.item_diamond_menber) {

    var index = 0

    init {
        setOnItemClickListener { adapter, view, position ->
            index = position
            notifyDataSetChanged()
            onItemClick(data[position])
        }
    }

    override fun convert(helper: BaseViewHolder, item: ExtendEntity) {
        val tvDiamondName = helper.setText(R.id.tvDiamondName, "${item.name}${item.validDate}天")
            .getView<TextView>(R.id.tvDiamondName)
        val tvDiamondPrice = helper.setText(R.id.tvDiamondPrice, SpanUtil.setMoneySymbol(item.price, false))
            .getView<TextView>(R.id.tvDiamondPrice)
        val tvDiamondValidDate = helper.setText(R.id.tvDiamondValidDate, "仅${div(item.price.toDouble(), item.validDate.toDouble())}元/天")
            .getView<TextView>(R.id.tvDiamondValidDate)
        val linearLayout3 = helper.getView<LinearLayout>(R.id.linearLayout3)
        if (index == getItemPosition(item)) {
            index = getItemPosition(item)
            onItemClick(data[index])
            tvDiamondName.setTextColor(context.resources.getColor(R.color.white))
            tvDiamondPrice.setTextColor(context.resources.getColor(R.color.white))
            tvDiamondValidDate.setTextColor(context.resources.getColor(R.color.white))
            linearLayout3.setBackgroundResource(R.drawable.bg_diamond_select)
        } else {
            tvDiamondName.setTextColor(context.resources.getColor(R.color.B99))
            tvDiamondPrice.setTextColor(context.resources.getColor(R.color.RE8))
            tvDiamondValidDate.setTextColor(context.resources.getColor(R.color.B99))
            linearLayout3.setBackgroundResource(R.drawable.bg_diamond)
        }
    }

    fun div(v1: Double, v2: Double): Double {
        val b1 = BigDecimal(java.lang.Double.toString(v1))
        val b2 = BigDecimal(java.lang.Double.toString(v2))
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).toDouble()
    }

}